package com.tourism.website.articlemanage.controller;
import com.tourism.website.apis.ArticleManageApis;
import com.tourism.website.articlemanage.service.IArticleCategoryService;
import com.tourism.website.articlemanage.service.IArticleImgService;
import com.tourism.website.articlemanage.service.IArticleService;
import com.tourismwebsite.articlemanage.dto.ArticleCategoryManageDTO;
import com.tourismwebsite.articlemanage.dto.ArticleManageDTO;
import com.tourismwebsite.vo.JsonVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kevin
 * @since 2024-06-11
 */
@RestController
@RequestMapping("/articleManage/")
@Api(tags = "管理员文章管理")
public class ArticleManageController implements ArticleManageApis {
    @Resource
    IArticleService iArticleService;
    @Resource
    IArticleCategoryService iArticleCategoryService;

    //更新文章状态
    //0通过，1不通过，2审批中
    @PostMapping("update-article-status")
    @Override
    @ApiOperation(value = "审核文章")
    public JsonVO<Boolean> updateArticleStatus(Integer articleId,String status,Integer reviewer) {
        Boolean result = iArticleService.updateByArticleId(articleId,status,reviewer);
        if (result)
            return JsonVO.success(result);
        return JsonVO.create(null,9999,"文章不存在");
    }
    //根据文章Id查询文章
    @GetMapping("query-article-content")
    @Override
    @ApiOperation(value = "查询文章")
    @ApiImplicitParam(name="articleId",value = "文章ID",paramType="query")
    public JsonVO<List<ArticleManageDTO>> queryArticle(Integer articleId) {
        List<ArticleManageDTO> result = iArticleService.queryArticleManage(articleId);
        if (!result.isEmpty()) {
            return JsonVO.success(result);
        } else {
            return JsonVO.create(null,9999,"文章不存在");
        }
    }
    //文章删除(批量)
    @DeleteMapping("delete-article-content")
    @Override
    @ApiOperation(value = "管理员删除文章（批量）")
    public JsonVO<Boolean> deleteArticle(@RequestParam List<Integer> articleIds) {
        boolean allDeleted = true;
        for (Integer articleId : articleIds) {
            if (!iArticleService.deleteArticleManage(articleId)) {
                allDeleted = false;
                // 可以选择在这里进行日志记录或其他处理
            }
        }
        if (allDeleted) {
            return JsonVO.success(true);
        } else {
            return JsonVO.fail(null);
        }
    }

    //从种类（category）中查询到该分类(category)中的所有文章ID
    @PostMapping("query-article-category")
    @Override
    @ApiOperation(value = "根据种类查询文章ID")
    public JsonVO<List<ArticleCategoryManageDTO>> queryCategoryToArticleId(Integer categoryId) {
        List<ArticleCategoryManageDTO> result = iArticleCategoryService.queryCategoryToArticleID(categoryId);
        return JsonVO.success(result);
    }
    //查询文章的名字和发布的时间
    @PostMapping("query-article-nameAndTime")
    @Override
    @ApiOperation(value = "查询文章的名字和发布时间")
    public JsonVO<List<ArticleManageDTO>> queryArticleNameAndTime(Integer articleId) {
        List<ArticleManageDTO> result = iArticleService.queryNameAndTime(articleId);

        return JsonVO.success(result);
    }
}
